import { Component, OnInit } from '@angular/core'
import { Location } from '@angular/common'
import { ActivatedRoute, Router, Params } from '@angular/router'
import { Observable } from 'rxjs'
import { switchMap, concatMap, tap, take } from 'rxjs/operators'
import { NzMessageService } from 'ng-zorro-antd'
import { PageService } from '@shared/services/page.service'
import { StoreService } from '../../store.service'
import { ComWindow } from '@shared/classes/ComWindowClass'
import { EStoreChannelRouteName } from '../../new/qrcode-channel/qrcode-channel.interface'
import { INewPageColumn } from '@shared/interfaces/common'
import { IQRRouteOfStore, ICardRouteOfStore, IECRouteOfStore, IRouteOfStore } from '@shared/interfaces/store'
import { I18NService } from '@core'
import { StoreOpenChannelComponent } from './store-open-channel.component'
import { StoreEditChannelComponent } from './store-edit-channel.component'
import { StoreDataService } from '../../store-data.service'
import { IMCC } from '@shared/components/mcc-select/mcc-select.interface'

@Component({
  selector: 'app-store-channel',
  templateUrl: './channel.component.html',
})
export class StoreChannelComponent implements OnInit {
  pages: INewPageColumn[] = []

  constructor(
    private location: Location,
    private router: Router,
    private route: ActivatedRoute,
    private msgSrv: NzMessageService,
    private storeService: StoreService,
    private pageService: PageService,
    private i18n: I18NService,
    private storeDataService: StoreDataService,
  ) {}

  ngOnInit() {
    this.initColumns()
  }

  initColumns(): void {
    let key: string
    this.route.params
      .pipe(
        tap(params => {
          switch (params.category) {
            case 'qr':
              key = 'BIZ.ACQUIRER.OTHER.QR_CHANNELS'
              break
            case 'card':
              key = 'BIZ.ACQUIRER.OTHER.CARD_CHANNELS'
              break
            case 'ec':
              key = 'BIZ.ACQUIRER.OTHER.EC_CHANNELS'
              break
            default:
              this.router.navigateByUrl('/exception/404')
              break
          }
        }),
        switchMap(() => this.route.data),
        take(1),
      )
      .subscribe(data => {
        let component: ComWindow
        switch (data.routeName) {
          case EStoreChannelRouteName.OpenChannel:
            component = new ComWindow(StoreOpenChannelComponent, { title: key })
            break
          case EStoreChannelRouteName.EditChannel:
            component = new ComWindow(StoreEditChannelComponent, { title: key })
            break
          default:
            throw new Error('unsupported route name')
        }
        this.pages = [
          {
            component,
          },
        ]
      })
  }

  submit(payload: IRouteOfStore): void {
    const route: IRouteOfStore = payload
    if (route.mcc) {
      const mcc = route.mcc as IMCC
      route.mcc = mcc.mccCode
    }
    const save$ = this.route.params.pipe(
      switchMap(params => {
        switch (params.category) {
          case 'qr':
            return this.saveQRChannel(params, route)
          case 'card':
            return this.saveCardChannel(params, route)
          case 'ec':
            return this.saveECChannel(params, route)
          default:
            break
        }
      }),
    )

    save$.subscribe(result => {
      /** 成功后不需要路由守卫 */
      this.pageService.triggerGuardAlive(false)
      this.msgSrv.success(this.trans('GENERAL.NOTIFICATION.UPDATE_SUCCESS'))
      this.location.back()
      setTimeout(() => {
        window.location.reload()
      }, 1000)
    })
  }

  private saveQRChannel(params: Params, route: IQRRouteOfStore): Observable<IRouteOfStore> {
    return this.route.data.pipe(
      concatMap(data => {
        switch (data.routeName) {
          case EStoreChannelRouteName.OpenChannel:
            return this.storeService.createQRRoute(params.id, route)
          case EStoreChannelRouteName.EditChannel:
            return this.storeService.updateQRRoute(params.id, params.cid, route)
          default:
            throw new Error('unsupported route name')
        }
      }),
    )
  }

  private saveCardChannel(params: Params, route: ICardRouteOfStore): Observable<IRouteOfStore> {
    return this.route.data.pipe(
      concatMap(data => {
        switch (data.routeName) {
          case EStoreChannelRouteName.OpenChannel:
            return this.storeService.createCardRoute(params.id, route)
          case EStoreChannelRouteName.EditChannel:
            return this.storeService.updateCardRoute(params.id, params.cid, route)
          default:
            throw new Error('unsupported route name')
        }
      }),
    )
  }

  private saveECChannel(params: Params, route: IECRouteOfStore): Observable<IRouteOfStore> {
    return this.route.data.pipe(
      concatMap(data => {
        switch (data.routeName) {
          case EStoreChannelRouteName.OpenChannel:
            return this.storeService.createECRoute(params.id, route)
          case EStoreChannelRouteName.EditChannel:
            return this.storeService.updateECRoute(params.id, params.cid, route)
          default:
            throw new Error('unsupported route name')
        }
      }),
    )
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
